<?php
/**
 * Note: 应用渠道CPA
 * Date: 2021/12/30
 */
namespace App\Http\Controllers\Admin;

use App\Models\Game\Center\AppSourceCpa;
use App\Models\Mongo\DailyLog\DayCpaCost;
use App\Services\Promo\BaiduService;
use App\Services\Promo\DouyinService;
use App\Services\Promo\KuaiShouService;
use App\Services\Promo\TencentService;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Validation\Rule;

class AppSourceCpaController extends CurdController
{
    protected string $classModel = AppSourceCpa::class;

    protected string $allowChannels = "kuaishou,douyin,baidu,tencent,uc,custom";

    protected array $searchFields = [
        'source' => self::FIELD_TYPE_NUMERIC,
        'channel' => self::FIELD_TYPE_NORMAL,
    ];

    protected array $orderFields = [
        'source' => 'desc',
    ];

    /**
     * 保存之前校验
     * @param Request $request
     */
    public function storeBefore(Request $request)
    {
        $request->validate([
            'source' => [
                'required',
                'numeric',
                Rule::unique('gameCenter.app_source_cpa')->where(function ($query) use ($request) {
                    // 单个渠道只能绑定一个广告主ID
                    return $query->where('advertiser_id', $request->advertiser_id);
                }),
            ],
            'channel' => "required|string|in:{$this->allowChannels}",
            'rebate' => 'sometimes|numeric',
            'formula' => 'required_if:channel,custom|string',
        ], [
            'source.unique' => '当前渠道已绑定该广告主',
        ]);
    }

    /**
     * 更新之前校验
     * @param Request $request
     */
    public function updateBefore(Request $request)
    {
        $request->validate([
            'source' => [
                'required',
                'numeric',
                Rule::unique('gameCenter.app_source_cpa')->ignore($request->id)->where(function ($query) use ($request) {
                    // 单个渠道只能绑定一个广告主ID
                    return $query->where('advertiser_id', $request->advertiser_id);
                }),
            ],
            'channel' => "required|string|in:{$this->allowChannels}",
            'rebate' => 'sometimes|numeric',
            'formula' => 'required_if:channel,custom|string',
        ], [
            'source.unique' => '当前渠道已绑定该广告主',
        ]);
    }

    /**
     * @return array
     */
    public function configs(
        KuaiShouService $kuaiShouService,
        DouyinService $douyinService,
        BaiduService $baiduService,
        TencentService $tencentService)
    {
        // TODO 缓存数据结构调整, 接入时需确认
        return [
            AppSourceCpa::CHANNEL_KUAISHOU => array_merge(config('promo.kuaishou'), [
                'advertisers' => $kuaiShouService->get($kuaiShouService::KEY_ADVERTISERS) ?: [],
            ]),
            AppSourceCpa::CHANNEL_DOUYIN => array_merge(config('promo.douyin'), [
                'advertisers' => $douyinService->get($douyinService::KEY_ADVERTISERS) ?: [],
            ]),
            AppSourceCpa::CHANNEL_BAIDU => array_merge(config('promo.baidu'), [
                'advertisers' => $baiduService->get($baiduService::KEY_ADVERTISERS) ?: [],
            ]),
            AppSourceCpa::CHANNEL_TENCENT => array_merge(config('promo.tencent'), [
                'advertisers' => $tencentService->get($tencentService::KEY_ADVERTISERS) ?: [],
            ]),
            AppSourceCpa::CHANNEL_UC => [
                'advertiser_ids' => array_map(function($row) {
                    return [
                        'id' => $row['target'] ?: $row['username'],
                        'name' => '-',
                    ];
                }, config('promo.uc', [])),
            ],
            // AppSourceCpa::CHANNEL_DOUYIN.'2' => array_merge(config('promo.douyin2'), [
                // 'advertiser_ids' => $douyin2Service->get($douyin2Service::KEY_ADVERTISER_IDS, []),
            // ]),
        ];
    }

    /**
     * CPA消费数据
     * @param Request $request
     * @return \Illuminate\Http\Resources\Json\AnonymousResourceCollection
     */
    public function dayCpaCost(Request $request)
    {
        $lists = DayCpaCost::ofFieldNumeric('source', $request->source)
            ->ofField('channel', $request->channel)
            ->ofField('day', $request->day)
            ->orderByDesc('day')
            ->paginate((int) $request->pageSize);
        return JsonResource::collection($lists);
    }
}
